package com.xinchuang.controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xinchuang.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.xinchuang.utils.CheckUtil;
import com.xinchuang.comment.R;
import com.xinchuang.comment.ResultCode;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;

import com.xinchuang.utils.DateUtils;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.lang3.StringUtils;
import com.xinchuang.service.PublicService;

/**
 * PublicController
 *
 * @date 2023-08-25
 */
@RestController
@RequestMapping("/public")
@Api(value = "共有", tags = "共有")
public class PublicController {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    private PublicService publicService;

    /**
     * 上传文件接口  函数名称uploadFile  功能描述:
     *
     * @param fileData 中文名:文件
     * @return 返回参数{}
     */
    @PostMapping("/uploadFile")
    @ApiOperation("上传文件")
    public R uploadFile(@RequestParam(value = "fileData", required = false) MultipartFile fileData) {
        String fileresult = "";
        fileresult = CheckUtil.checkFile(fileData, "文件", true);
        if (StringUtils.isNotEmpty(fileresult)) {
            return R.fail(fileresult);
        }
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        String result = "";
        if (result.length() > 0) {
            return R.fail("入参：" + result);
        } else {
            // 调用业务层代码
            String r = publicService.uploadFile(
                    fileData
            );
            Set<String> aSet = new HashSet<>();
            JSONObject o = JSON.parseObject(r);
            if (o == null) {
                o = new JSONObject();
            }
            result = jsonLoop(o.get("data"), "", methodName + "AC", aSet);
            result = result + uploadFileACB(aSet);
            if (result.length() > 0) {
                return R.fail("出参：" + result);
            }
            return JSON.parseObject(r, R.class);
        }
    }

    /**
     * 上传影像接口  函数名称uploadImg  功能描述:
     *
     * @param imgData 中文名:影像
     * @return 返回参数{}
     */
    @PostMapping("/uploadImg")
    @ApiOperation("上传影像")
    public Result uploadImg(
            @RequestParam(value = "imgData", required = false) MultipartFile imgData
    ) {
        // 调用业务层代码
        return publicService.uploadImg(
                imgData
        );
    }

    /**
     * 上传影像接口  函数名称uploadImg  功能描述:
     *
     * @param imgData 中文名:影像
     * @return 返回参数{}
     */
    @PostMapping("/uploadTif")
    @ApiOperation("上传影像")
    public Result uploadTif(
            @RequestParam(value = "imgData", required = false) MultipartFile imgData
    ) {
        // 调用业务层代码
        return publicService.uploadTif(
                imgData
        );
    }

    /**
     * 获取国家类型列表接口  函数名称countryList  功能描述:
     *
     * @param jsonObject 中文名:通用实体类
     * @return 返回参数{}
     */
    @PostMapping("/countryList")
    @ApiOperation("获取国家类型列表")
    public R countryList(@RequestBody JSONObject jsonObject) {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        Set<String> sSet = new HashSet<>();
        String result = jsonLoop(jsonObject, "", methodName + "SC", sSet);
        result = result + countryListACS(sSet);
        if (result.length() > 0) {
            return R.fail("入参：" + result);
        } else {
            // 调用业务层代码
            String r = publicService.countryList(jsonObject);
            Set<String> aSet = new HashSet<>();
            JSONObject o = JSON.parseObject(r);
            if (o == null) {
                o = new JSONObject();
            }
            result = jsonLoop(o.get("data"), "", methodName + "AC", aSet);
            result = result + countryListACB(aSet);
            if (result.length() > 0) {
                return R.fail("出参：" + result);
            }
            return JSON.parseObject(r, R.class);
        }
    }

    @PostMapping("/uploadBase")
    public Result uploadBase(
            @RequestParam(value = "imgData", required = false) MultipartFile imgData,
            @RequestParam(value = "tarId", required = false) String tarId) {
        return publicService.uploadTypeTif(imgData, tarId, 0);
    }

    @PostMapping("/uploadNew")
    public Result uploadNew(
            @RequestParam(value = "imgData", required = false) MultipartFile imgData,
            @RequestParam(value = "tarId", required = false) String tarId) {
        return publicService.uploadTypeTif(imgData, tarId, 1);
    }

    @PostMapping("/uploadResult")
    public Result uploadResult(
            @RequestParam(value = "imgData", required = false) MultipartFile imgData,
            @RequestParam(value = "tarId", required = false) String tarId) {
        return publicService.uploadTypeTif(imgData, tarId, 2);
    }

    /**
     * 登录接口  函数名称login  功能描述:
     *
     * @param jsonObject 中文名:通用实体类
     * @return 返回参数{}
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result login(@RequestBody JSONObject jsonObject) {
        return publicService.login(jsonObject);
    }


    public static String jsonLoop(Object object, String result, String methodName, Set<String> set) {
        Object v = null;
        if (object instanceof JSONArray || object instanceof ArrayList) {
            JSONArray jsonArray = new JSONArray();
            if (object instanceof ArrayList) {
                jsonArray = JSONArray.parseArray(JSON.toJSONString(object));
            } else {
                jsonArray = (JSONArray) object;
            }
            for (int i = 0; i < jsonArray.size(); i++) {
                result = jsonLoop(jsonArray.get(i), result, methodName, set);
                if (StringUtils.isNotEmpty(result)) {
                    return result;
                }
            }
        }
        if (object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                Object o = entry.getValue();
                if (o instanceof JSONArray || o instanceof ArrayList) {
                    result += selectCheckMathod(entry.getKey(), entry.getValue(), methodName, set);
                    result = jsonLoop(o, result, methodName, set);
                    if (StringUtils.isNotEmpty(result)) {
                        return result;
                    }
                } else if (o instanceof JSONObject) {
                    result = jsonLoop(o, result, methodName, set);
                    if (StringUtils.isNotEmpty(result)) {
                        return result;
                    }
                } else {
//                   FIXME: 未知类型
                }
                v = entry.getValue();
                result += selectCheckMathod(entry.getKey(), v, methodName, set);
            }
        }
        return result;
    }

    private static String selectCheckMathod(String key, Object value, String methodName, Set<String> set) {
        String result = "";
        if ("uploadFileSC".equals(methodName)) {
            result = uploadFileSC(key, value, set);
        }
        if ("uploadFileAC".equals(methodName)) {
            result = uploadFileAC(key, value, set);
        }
        if ("uploadImgSC".equals(methodName)) {
            result = uploadImgSC(key, value, set);
        }
        if ("uploadImgAC".equals(methodName)) {
            result = uploadImgAC(key, value, set);
        }
        if ("countryListSC".equals(methodName)) {
            result = countryListSC(key, value, set);
        }
        if ("countryListAC".equals(methodName)) {
            result = countryListAC(key, value, set);
        }
        if ("loginSC".equals(methodName)) {
            result = loginSC(key, value, set);
        }
        if ("loginAC".equals(methodName)) {
            result = loginAC(key, value, set);
        }
        return result;
    }


    private static String uploadFileSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("fileData".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String uploadFileAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("fileName".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "名称", isNull, 0, 50);
        }
        if ("fileType".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "类型", isNull, 0, 50);
        }
        if ("fileSize".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "大小", isNull, 0, 50);
        }
        if ("uploadTime".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "上传时间", isNull, 0, 50);
        }
        if ("fileUrl".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "地址", isNull, 0, 999);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String uploadImgSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("imgData".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String uploadImgAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if ("imgName".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "名称", isNull, 0, 50);
        }
        if ("imgType".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "类型", isNull, 0, 50);
        }
        if ("imgSize".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "大小", isNull, 0, 50);
        }
        if ("imgTime".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "上传时间", isNull, 0, 50);
        }
        if ("fileUrl".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "地址", isNull, 0, 999);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String countryListSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("type".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "类型", isNull, 0, 50);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String countryListAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if ("data".equals(key)) {
            result += CheckUtil.checkJSONArray(JSONArray.parseArray(JSON.toJSONString(value)), "承载数据", isNull);
        }
        if ("label".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "名称", isNull, 0, 50);
        }
        if ("value".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标识", isNull, 0, 50);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String loginSC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("userName".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "用户名", isNull, 0, 50);
        }
        if ("password".equals(key)) {
            isNull = true;
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "密码", isNull, 0, 50);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String loginAC(String key, Object value, Set<String> set) {
        String result = "";
        Boolean isNull = false;
        if ("code".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkBigDecimal(value == null ? "" : value.toString(), "状态码", isNull, "0", "100000");
        }
        if ("msg".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "返回消息", isNull, 0, 255);
        }
        if ("success".equals(key)) {
            // 判断参数类型: String  num boolean datetime
        }
        if ("xName".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "姓名", isNull, 0, 50);
        }
        if ("role".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "类型", isNull, 0, 50);
        }
        if ("token".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "钥匙", isNull, 0, 255);
        }
        if ("id".equals(key)) {
            // 判断参数类型: String  num boolean datetime
            result += CheckUtil.checkString(value == null ? "" : value.toString(), "标识", isNull, 0, 255);
        }
        if (isNull) {
            set.add(key);
        }
        return result;
    }

    private static String uploadFileACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String uploadFileACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    private static String uploadImgACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String uploadImgACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    private static String countryListACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        list.add("type");
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String countryListACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    private static String loginACS(Set<String> set) {
        List<String> list = new ArrayList<>();
        list.add("password");
        list.add("userName");
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "入参");
    }

    private static String loginACB(Set<String> set) {
        List<String> list = new ArrayList<>();
        if (set.size() == list.size()) {
            return "";
        }
        return getCollectionDifferentValue(list, set, "出参");
    }

    /**
     * 获取集合中的不同值
     *
     * @param list
     * @param set
     * @return
     */
    private static String getCollectionDifferentValue(List<String> list, Set<String> set, String flag) {
        String result = "";
        for (String str : list) {
            if (!set.contains(str)) {
                result += str + ",";
            }
        }
        if (StringUtils.isNotEmpty(result)) {
            result = StringUtils.strip(result, ",");
            result = "---" + flag + ":" + result + "为必填参数";
        }
        return result;
    }
}
